package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PlanDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Evaluate;
import com.itheima.pojo.Plan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service(interfaceClass = PlanService.class)
@Transactional
public class PlanServiceImpl implements PlanService {
    @Autowired
    private PlanDao PlanDao;


    @Override
    public void add(Plan plan) {
        PlanDao.add(plan);
    }

    @Override
    public void delete(Integer id) {
        PlanDao.deleteById(id);
    }

    @Override
    public PageResult findPage(Map map) {
        Integer currentPage = (Integer) map.get("currentPage");//当前页
        Integer pageSize = (Integer) map.get("pageSize");//每页条数
        String queryString = (String) map.get("queryString");//查询条件
        String queryoption = (String) map.get("queryoption");//查询条件

        //使用分页助手插件实现分页查询
        //如果条件不为空和空字符串 那么查询的页数为1
        if (queryString != null && !"".equals(queryString)   ) {
            map.put("currentPage", 1);
        }
        System.out.println("map = " + map);
        PageHelper.startPage(currentPage, pageSize);
        Page<Plan> page = PlanDao.findByCondition(map);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public Plan findById(Integer id) {
        return PlanDao.findById(id);
    }

    @Override
    public void edit(Plan plan) {
        PlanDao.edit(plan);
    }

    @Override
    public List<Map> findCategory() {
        return PlanDao.findCategory();
    }
}
